package cn.edu.ldu.views.modules.controller;

import cn.edu.ldu.http.rqtUtils;
import cn.edu.ldu.util.Consts;
import cn.edu.ldu.views.modules.entity.UserIpEntity;
import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Objects;

public class UserIpController {

    public static UserIpEntity getUserLastIp(Long userId, String token) throws Exception {
        List<UserIpEntity> userIps = getUserIps(userId, token);
        return userIps != null ? userIps.get(0) : null;
    }


    public static List<UserIpEntity> getUserIps(Long userId, String token) throws Exception {

        JSONObject jsonObject = rqtUtils.httpclientGet(new StringBuffer(Consts.getUserLastIp).append("/").append(userId), token);
        if(jsonObject.getString("msg").equals("success") && jsonObject.getString("list").equals("")){
            return JSONObject.parseArray(jsonObject.getString("list"), UserIpEntity.class);
        }
        return null;
    }

    public static void addUserIp(UserIpEntity userIpEntity, String token) throws Exception {

        String jsonString = JSONObject.toJSONString(userIpEntity);

        String result = rqtUtils.httpclientPost(new StringBuffer(Consts.addUserIpInfo), jsonString, token);

    }

}
